﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using DevelopTool.Bll.LocalDbBlls;
using DevelopTool.Common;
using DevelopTool.Model.CommonModels;
using DevelopTool.Model.Enums;
using DevelopTool.Model.NoteModels;
using LiteDB;

namespace DevelopTool.Bll.ManageMenuBlls
{
    /// <summary>
    /// 管理页菜单操作
    /// </summary>
    public class ManageMenuBll
    {
        /// <summary>
        /// 获取菜单列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="nodeType"></param>
        /// <returns></returns>
        public List<T> GetList<T>(MenuNodeType nodeType = MenuNodeType.Unknown) where T : ManageMenu
        {
            using(var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var models = db.GetCollection<T>().FindAll();

                if(nodeType != MenuNodeType.Unknown)
                {
                    models = models.Where(it => it.NodeType == nodeType);
                }

                return models.OrderByDescending(it => it.CreateTime).ToList();
            }
        }

        /// <summary>
        /// 根据名称查找菜单节点id集合
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public List<T> GetIdListByName<T>(string name) where T : ManageMenu
        {
            using(var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<T>();

                return col.Find(it => !string.IsNullOrEmpty(it.NodeName) && it.NodeName.IndexOf(name,StringComparison.OrdinalIgnoreCase) > -1 && it.NodeType == MenuNodeType.Node).ToList();
            }
        }


        /// <summary>
        /// 根据时间字符串查找菜单节点id集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="timeStr"></param>
        /// <returns></returns>
        public List<T> GetIdListByTime<T>(string timeStr) where T : ManageMenu
        {
            using(var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<T>();

                return col.Find(it => it.CreateTime.ToString("yyyyMMddHHmmss").Contains(timeStr) && it.NodeType == MenuNodeType.Node).ToList();
            }
        }


        /// <summary>
        /// 检查同一个目录中是否存在相同名称的节点
        /// </summary>
        /// <param name=></param>
        /// <returns></returns>
        public bool CheckName<T>(string nodeName,int nodePid) where T : ManageMenu
        {
            using(var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<T>();
                return col.Exists(it => it.NodePid == nodePid && it.NodeName.Equals(nodeName));
            }
        }


        /// <summary>
        /// 填充实体对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="manageMenu"></param>
        /// <returns></returns>
        public T GetExample<T>(ManageMenu manageMenu) where T : ManageMenu
        {
            using(var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<T>();

                var model = col.FindById(manageMenu.Id);
                if(model == null)
                {
                    model = Activator.CreateInstance<T>();
                }

                foreach(PropertyInfo item in typeof(ManageMenu).GetProperties())
                {
                    item.SetValue(model,item.GetValue(manageMenu,null),null);
                }
                return model;
            }

        }

        /// <summary>
        /// 简单保存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public T SimpleSave<T>(T t) where T : ManageMenu
        {
            using(var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<T>();

                t.UpdateTime = DateTime.Now;

                col.Upsert(t);

                return col.FindById(t.Id);
            }
        }


    }
}
